/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Home.java
 *
 * Created on 17-03-2011, 11:18:06
 */
package GUI;

import interact.connect;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.netbeans.lib.awtextra.AbsoluteConstraints;

/**
 *
 * @author UnderGround
 */
public class Home extends javax.swing.JFrame {
    /** Creates new form Home */
    public Home() {
         Image icon = getToolkit().getImage(getClass().getResource("/images/soft_icon.png"));
        setIconImage(icon);
//          new interact.progress(null,true);
        connect.connection();
        initComponents();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }

        javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                time.setText(new Date().toString());
            }
        });
        t.start();

    }
    private Image image;
//    Icon leafIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/home.png")));
//    Icon openIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/home2.png")));
//    Icon closedIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/home.png")));

    private void expand(JTree tree, TreePath path) {
        TreeNode node = (TreeNode) path.getLastPathComponent();
        if (node.getChildCount() > 0) {
            Enumeration e = node.children();
            while (e.hasMoreElements()) {
                TreeNode n = (TreeNode) e.nextElement();
                expand(tree, path.pathByAddingChild(n));
            }
        }
        tree.expandPath(path);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        //image = new ImageIcon("src/Images/library.jpg").getImage();
        image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/library.jpg"));
        ContentPanel = new interact.ImagePanel(image);
        jScrollPane1 = new javax.swing.JScrollPane();
        DefaultMutableTreeNode library = new DefaultMutableTreeNode("HOME");
        createNodes(library);
        Tree = new javax.swing.JTree(library);
        Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        // Update only one tree instance
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)Tree.getCellRenderer();
        //cellRenderer.setLeafIcon(leafIcon);
        //cellRenderer.setClosedIcon(closedIcon);
        //cellRenderer.setOpenIcon(openIcon);

        cellRenderer.setBackgroundNonSelectionColor(new Color(255, 255, 221));
        cellRenderer.setBackgroundSelectionColor(new Color(0, 0, 128));
        cellRenderer.setBorderSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.white);
        cellRenderer.setTextNonSelectionColor(Color.blue);

        //UIManager.put("Tree.leafIcon", leafIcon);
        //UIManager.put("Tree.openIcon", openIcon);
        //UIManager.put("Tree.closedIcon", closedIcon);
        Tree = new javax.swing.JTree(library);
        Tree.setCellRenderer(new CellRenderer());
        ;
        pnl = new javax.swing.JPanel();
        time = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Library management \n ");
        setResizable(false);

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));
        jDesktopPane1.setPreferredSize(new java.awt.Dimension(715, 490));

        ContentPanel.setBackground(new java.awt.Color(204, 204, 204));
        ContentPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        ContentPanel.setPreferredSize(new java.awt.Dimension(715, 490));
        ContentPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        ContentPanel.setBounds(210, 100, 710, 500);
        jDesktopPane1.add(ContentPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Tree.setBorder(javax.swing.BorderFactory.createCompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        Tree.setFont(new java.awt.Font("Tahoma", 1, 11));
        Tree.setRowHeight(30);
        Tree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                TreeMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(Tree);
        expand(Tree, Tree.getPathForRow(0));

        jScrollPane1.setBounds(0, 100, 210, 500);
        jDesktopPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        pnl.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnl.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        pnl.add(time, new org.netbeans.lib.awtextra.AbsoluteConstraints(914, 2, 162, 24));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/banner_gui.png"))); // NOI18N
        pnl.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 930, 100));

        pnl.setBounds(0, 0, 930, 100);
        jDesktopPane1.add(pnl, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jMenuBar1.setBackground(new java.awt.Color(235, 233, 233));

        jMenu1.setText("File");

        jMenuItem1.setText("Log out");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Change infomation");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Exit");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Managers");

        jMenuItem5.setText("Book management");
        jMenu2.add(jMenuItem5);

        jMenuItem4.setText("Empoyee management");
        jMenu2.add(jMenuItem4);

        jMenuItem8.setText("Borrower management");
        jMenu2.add(jMenuItem8);

        jMenuBar1.add(jMenu2);

        jMenu4.setText("About");

        jMenuItem6.setText("About us");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem6);

        jMenuItem7.setText("Help");
        jMenu4.add(jMenuItem7);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 911, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 618, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-927)/2, (screenSize.height-657)/2, 927, 657);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        AboutUs aboutUs = new AboutUs(this, true);
        aboutUs.setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
          new ShowMessageDialog(null, true, "Are you sure ?", "Validation Message", ShowMessageDialog.CONFIRM);
        if (ShowMessageDialog.STATUS == ShowMessageDialog.OK) {
        System.exit(0);
         }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        new ShowMessageDialog(null, true, "Are you sure ?", "Validation Message", ShowMessageDialog.CONFIRM);
        if (ShowMessageDialog.STATUS == ShowMessageDialog.OK) {
        this.dispose();
        frmLogin log = new frmLogin();
        log.setVisible(true);
         }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void TreeMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TreeMousePressed
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) Tree.getLastSelectedPathComponent();
        Object nodeInfo = node.getUserObject();

        if (node.isLeaf()) {
            System.out.println(nodeInfo.toString());
            if (nodeInfo == "My account") {
                frmAdministrator admin = new frmAdministrator();
                callPanel(admin);
            }
            if (nodeInfo == "View Employee") {
                frmEmployeeManager emp = new frmEmployeeManager();
                callPanel(emp);
            }

            if (nodeInfo == "Book Manager") {
                BookManager book = new BookManager();
                callPanel(book);
            }
            if (nodeInfo == "Add Department") {
            }
            if (nodeInfo == "Category manager") {
                frmCategoryManager category = new frmCategoryManager();
                callPanel(category);
            }
            if (nodeInfo == "Publisher manager") {
                frmPublisherManager publisher = new frmPublisherManager();
                callPanel(publisher);
            }
            if (nodeInfo == "Search book") {
                frmSearch search = new frmSearch();
                callPanel(search);
            }
            if (nodeInfo == "Department manager") {
                frmDepartmentManager department = new frmDepartmentManager();
                callPanel(department);
            }

            if (nodeInfo == "Change password") {
                frmChangePassword change = new frmChangePassword();
                callPanel(change);
            }

            if (nodeInfo == "IssueBook ") {
                frmChangePassword change = new frmChangePassword();
                callPanel(change);
            }
            if (nodeInfo == "Return book") {
                frmChangePassword change = new frmChangePassword();
                callPanel(change);
            }


            if (nodeInfo == "Borrow management") {
                BorrowManagment bm = new BorrowManagment();
                callPanel(bm);
            }
            if (nodeInfo == "Statistical reports") {
                frmReport report = new frmReport();
                callPanel(report);
            }
        }
}//GEN-LAST:event_TreeMousePressed

    public static void callPanel(JPanel jpanel) {
        ContentPanel.removeAll();
        jpanel.setVisible(false);
        ContentPanel.add(jpanel, new AbsoluteConstraints(0, 0, ContentPanel.getWidth(), ContentPanel.getHeight()));
        jpanel.setVisible(true);
    }

    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode emp = null;
        DefaultMutableTreeNode acc = null;
        DefaultMutableTreeNode book = null;
        DefaultMutableTreeNode IssueBook = null;


        category = new DefaultMutableTreeNode("Employee manager");
        top.add(category);

        emp = new DefaultMutableTreeNode("View Employee");
        category.add(emp);

//        emp = new DefaultMutableTreeNode("Add employee");
//        category.add(emp);

        emp = new DefaultMutableTreeNode("Department manager");
        category.add(emp);

        category = new DefaultMutableTreeNode("Account manager");
        top.add(category);

        acc = new DefaultMutableTreeNode("My account");
        category.add(acc);

        acc = new DefaultMutableTreeNode("Change password");
        category.add(acc);

        category = new DefaultMutableTreeNode("Book management");
        top.add(category);

        book = new DefaultMutableTreeNode("Search book");
        category.add(book);

//        book = new DefaultMutableTreeNode("Add new book");
//        category.add(book);

        book = new DefaultMutableTreeNode("Book Manager");
        category.add(book);

        book = new DefaultMutableTreeNode("Publisher manager");
        category.add(book);

        book = new DefaultMutableTreeNode("Category manager");
        category.add(book);

        book = new DefaultMutableTreeNode("Borrow management");
        category.add(book);

        category = new DefaultMutableTreeNode("Statistical reports");
        top.add(category);
//
//
//         IssueBook = new DefaultMutableTreeNode("Borrow book");
//        category.add(IssueBook);
//
//         IssueBook = new DefaultMutableTreeNode("Return book");
//        category.add(IssueBook);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Home().setVisible(true);
            }
        });
    }
    public static String userLogin;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JPanel ContentPanel;
    private javax.swing.JTree Tree;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnl;
    private javax.swing.JLabel time;
    // End of variables declaration//GEN-END:variables
}

class CellRenderer extends DefaultTreeCellRenderer {

    @Override
    public Component getTreeCellRendererComponent(JTree tree,
            Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, sel,
                expanded, leaf, row, hasFocus);
        if (value.toString().equalsIgnoreCase("Home")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon_home.png")));
            setIcon(view);
        }
//
        if (value.toString().equalsIgnoreCase("View Employee")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/group.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Employee manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon_emp.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Account manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/administrator.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Book Management")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon_book.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Book Manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/book_icon.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Search book")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/search_1.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Add new book")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/add.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Change password")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/change_1.jpg")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("My account")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/my_acc.jpg")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Publisher manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/publisher.jpg")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Category manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/category.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Department manager")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon_depart.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Borrow management")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/borrow.png")));
            setIcon(view);
        }
        if (value.toString().equalsIgnoreCase("Statistical reports")) {
            ImageIcon view = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/report.png")));
            setIcon(view);
        }
        return this;

    }
}
